define(['zepto', 'utils', 'wxshare', 'JST', 'page/jst/movement-btn', 'page/jst/action-group', 'sui'], function($, utils, wxshare, JST, movementBind, actionGroup) {
  return {
    init: function(data) {
      this.roundId = data.roundId;
      this.batchId = data.batchId;
      this.type = data.type;
      this.categories = data.categories;
      this.equipments = data.equipments;
      this.day = data.day;
      
      this.coursewareDelete();
      this.dailyRest();
      this.batchLink();
      this.addAction();
      this.movementEdit();
      movementBind.init(window.__CTX__, data)
      actionGroup.init(window.__CTX__, data)
    },
    
    coursewareDelete: function() {
         var that = this;
         
        $(document).on('click','.close-module', function () {
            var dailyId = $(this).data('daily');
            var coursewareId = $(this).data('courseware');
            $.modal({
              text: '<div class="tab active" id="tab1">确定删除该动作</div>',
              buttons: [
                {
                  text: '确定',
                  onClick: function() {
                      that.bindDeleteClick(dailyId, coursewareId);
                  }
                },
                {
                  text: '取消'
                },
              ]
            })
        });
    },
    
    bindDeleteClick: function(dailyId, coursewareId) {
        var that = this;
        
        $.ajax({
            url: window.__CTX__.root + '/rounds/' + that.roundId + '/batch-coursewares/' + that.batchId + '/daily-coursewares/' + dailyId + '/coursewares/' + coursewareId,
            type: 'POST',
            data:{
                  '_method': 'DELETE',
                  'coursewareId': coursewareId
                 },
            success: function(res) {
                var res = JSON.parse(res)
                $.toast(res.message);
                window.location.reload();
            },
            fail: function() {
                $.toast("请求失败")
            }
        })
    },
    
    dailyRest: function() {
        var that = this;
        
        $(document).on('click','.daily-rest', function (e) {
            e.preventDefault();
            var rest = $(this).data("rest");
            var dailyId = $(this).data('daily');
            if(rest === 1) {
                $(this).hide();
                $('.reset-action-' + dailyId).show();
                $(".action-card-" + dailyId).hide();
                $(".daily-card-" + dailyId).show();
            } else {
                $.modal({
                    text: '<div class="tab active" id="tab1">确定将该日设为休息？</div>',
                    buttons: [
                      {
                        text: '确定',
                        onClick: function() {
                            that.bindRestClick(dailyId);
                        }
                      },
                      {
                        text: '取消'
                      },
                    ]
                  })
            }
            
        });
    },
    
    bindRestClick: function(dailyId) {
        var that = this;
        
        $.ajax({
            url: window.__CTX__.root + '/rounds/' + that.roundId + '/batch-coursewares/' + that.batchId + '/daily-coursewares/' + dailyId + '/rest',
            type: 'POST',
            data:{
                  '_method': 'PATCH'
                 },
            success: function(res) {
                var res = JSON.parse(res);
                $.toast(res.message);
                window.location.reload();
            },
            fail: function(res) {
                $.toast("请求失败")
            }
        })
    },
    
    batchLink: function() {
        var that = this;
        
        $(document).on('click','.batch-link', function () {
            $.modal({
              title: '发送课件',
              text: '<div class="list-block no-margin">' +
                      '<ul>'+
                      '<li>'+
                          '<div class="item-content">'+
                              '<div class="item-inner">'+
                                  '<div class="item-input"><textarea name="coachTips" placeholder="您有任何Tips,请在此输入"></textarea></div>'+
                              '</div>'+
                          '</div>'+
                      '</li>'+
                  '</ul>'+
              '</div>',
              buttons: [
                {
                  text: '确定',
                  onClick: function() {
                      that.bindLinkClick();
                  }
                },
                {
                  text: '取消'
                },
              ]
            })
        });
    },
    
    bindLinkClick: function() {
        var that = this;
        var coachTips = $("textarea[name='coachTips']").val().trim();
        
        if(that.type === 'WEEKLY') {
        	var currentData = {'_method': 'PATCH', 'type': that.type, 'coachTips': coachTips};
        } else {
        	var currentData = {'_method': 'PATCH', 'type': that.type, day: that.day, 'coachTips': coachTips};
        }
        
        $.ajax({
            url: window.__CTX__.root + '/rounds/' + that.roundId + '/batch-coursewares/' + that.batchId + '/link',
            type: 'POST',
            data:currentData,
            success: function(res) {
                var res = JSON.parse(res);
                $.toast(res.message);
                window.location.reload();
            },
            fail: function() {
                $.toast("请求失败");
            }
        })
    },
    
    addAction: function() {
        $(document).on("click", '.set-action', function(e) {
            e.preventDefault();
            var dailyId = $(this).data("daily");
            $(this).hide();
            $('.set-rest-' + dailyId).show();
            $(".daily-card-" + dailyId).hide();
            $(".action-card-" + dailyId).show();
        })
    },
    
    movementEdit: function() {
        var that = this;
        
        $(document).on('click','.movement-edit', function () {
            var groupAmount = !$(this).data("amount") ? '0' : $(this).data("amount");
            var movementNumber = !$(this).data("number") ? '0' : $(this).data("number");
            var maxWeight = !$(this).data("weight") ? '' : $(this).data("weight");
            var time = !$(this).data("time") ? '' : $(this).data("time");
            var dailyId = $(this).data("daily");
            var coursewareId = $(this).data("courseware");
            var remark = !$(this).data("remark") ? '' : $(this).data("remark");
            
            $.modal({
              title: '修改动作',
              text: '<div class="list-block no-margin">' +
                      '<ul>'+
                        '<li>'+
                            '<div class="item-content">'+
                                '<div class="item-inner">'+
                                    '<div class="item-title label">组数</div>'+
                                    '<div class="item-input"><input type="text" name="groupAmount" value="' + groupAmount + '" placeholder="请输入组数"></div>'+
                                '</div>'+
                            '</div>'+
                        '</li>'+
                        '<li>'+
                            '<div class="item-content">'+
                                '<div class="item-inner">'+
                                    '<div class="item-title label">动作次数</div>'+
                                    '<div class="item-input"><input type="text" name="movementNumber" value="' + movementNumber + '" placeholder="请输入动作次数"></div>'+
                                '</div>'+
                            '</div>'+
                        '</li>'+
                        '<li>'+
                          '<div class="item-content">'+
                            '<div class="item-inner">'+
                                '<div class="item-title label">最大重量</div>'+
                                '<div class="item-input"><input type="text" name="maxWeight" value="' + maxWeight + '" placeholder="请输入最大重量"></div>'+
                            '</div>'+
                        '</div>'+
                      '</li>'+
                      '<li>'+
                        '<div class="item-content">'+
                          '<div class="item-inner">'+
                              '<div class="item-title label">计时</div>'+
                              '<div class="item-input"><input type="text" name="time" value="' + time + '" placeholder="请输入计时"></div>'+
                          '</div>'+
                        '</div>'+
                      '</li>'+
	                  '<li>'+
	                    '<div class="item-content">'+
	                      '<div class="item-inner">'+
	                        '<div class="item-input"><textarea name="remark" placeholder="请输入备注">' + remark + '</textarea></div>'+
	                      '</div>'+
	                    '</div>'+
	                  '</li>'+
                    '</ul>'+
                '</div>',
                buttons: [
                  {
                    text: '确定',
                    onClick: function() {
                        that.bindMovementEdit(dailyId, coursewareId);
                  }
                },
                {
                  text: '取消'
                },
              ]
            })
        });
    },
    
    bindMovementEdit: function(dailyId, coursewareId) {
        var that = this;
        var groupAmount = $('input[name="groupAmount"]').val().trim();
        var movementNumber = $('input[name="movementNumber"]').val().trim();
        var maxWeight = $('input[name="maxWeight"]').val().trim();
        var time = $('input[name="time"]').val().trim();
        var remark = $('textarea[name="remark"]').val().trim();
        
        if(!groupAmount) {
      	  $("input[name='groupAmount']").val(0);
        } else if(isNaN(groupAmount)) {
            $.toast("组数请输入数字")
            return false;
        }
        
        if(!movementNumber) {
      	  $("input[name='movementNumber']").val(0);
        } else if(isNaN(movementNumber)) {
          $.toast("每组次数请输入数字")
          return false;
        }
        
        $.ajax({
            url: window.__CTX__.root + '/rounds/' + that.roundId + '/batch-coursewares/' + that.batchId + '/daily-coursewares/' + dailyId + '/coursewares/' + coursewareId,
            type: 'POST',
            data: {
                '_method': 'PATCH',
                'coursewareId': coursewareId,
                'groupAmount': groupAmount,
                'movementNumber': movementNumber,
                'maxWeight': maxWeight,
                'time': time,
                'remark': remark
            },
            success: function(res) {
                var res = JSON.parse(res);
                $.toast(res.message);
                window.location.reload();
            },
            fail: function() {
                $.toast("请求失败");
            }
        })
    }
  }
})